#include "stdafx.h"
#include "Button.h"

Button::Button(const std::string& buttonUp, const std::string& buttonDown, const std::string& buttonHover, const Vector2D<float>& position) : 
                                                                                                                                                GUIObject(),
                                                                                                                                                m_pListener(NULL),
                                                                                                                                                m_state(eReleased),
                                                                                                                                                m_isHovering(false)
{
    //Button
    m_pButtonUp = LoadResource<SDLTexture>("game_interface", buttonUp );
    m_pButtonDown = LoadResource<SDLTexture>("game_interface", buttonDown);
    m_pButtonHover = LoadResource<SDLTexture>("game_interface", buttonHover);

    if (!m_pButtonUp || !m_pButtonDown || !m_pButtonHover)
    {
        LOG_ERROR("Error initializing button");
    }
    else
    {
        Init(position, m_pButtonUp->GetDimension());
    }
}

Button::~Button()
{
}

bool Button::Update()
{
    return false;
}

void Button::Render(SDL_Renderer& renderer)
{          
    SDLTexture* pRenderTexture = NULL;
    switch (m_state)
    {
    case ePressed:
        pRenderTexture = m_pButtonDown;
        break;
    case eReleased:
        if (m_isHovering)
        {
            pRenderTexture = m_pButtonHover;
        }
        else
        {
            pRenderTexture = m_pButtonUp;
        }
        break;
    }
        
    if (pRenderTexture)
    {
        Vector2D<s32> size = pRenderTexture->GetDimension();
        SDL_Rect src = {0, 0, size.GetX(), size.GetY()};
        SDL_RenderCopyEx(&renderer, pRenderTexture->GetTexture(), &src, &GetRectangle(), 0, NULL, SDL_FLIP_NONE);
    }    
}

bool Button::OnMouseButtonPressed(const Vector2D<s32>& absolute, s32 button)
{
    if (m_pListener && IsLeftButton(button))
    {     
        ChangeState(ePressed);
    }

    return true;
}

bool Button::OnMouseButtonReleased(const Vector2D<s32>& absolute, s32 button)
{
    if (m_pListener && IsLeftButton(button))
    {     
        //If we release the mouse button, make sure we clicked it first
        if (m_state == ePressed)
        {
            ChangeState(eReleased);
            m_pListener->OnButtonClicked(GetID());
        }
    }
    
    return true;
}

void Button::OnMouseButtonUp(const Vector2D<s32>& absolute, s32 button)
{
    ChangeState(eReleased);
}

void Button::OnMouseIn()
{
    m_isHovering = true;
}

void Button::OnMouseOver()
{
    m_isHovering = true;
}

void Button::OnMouseOut()
{
    m_isHovering = false;
}

void Button::SetListener(ButtonListener* pListener)
{
    m_pListener = pListener;
}

void Button::ChangeState(ButtonState newState)
{
    if (newState != m_state)
    {
        m_state = newState;
    }
}
